package meijuli.com.join51.business.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.adapter.CustomAdapter;
import meijuli.com.join51.business.adapter.CustomHolder;
import meijuli.com.join51.business.model.Category;
import com.jhx.common.model.Event;

import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by Administrator on 2017/3/3.
 */

public class CategoryActivity extends BaseActivity{
    private CustomAdapter<Category> mAdapter;
    private List<Category> mCategoryList;
    private RecyclerView mRecyclerView;
    private TextView mTitleText;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_category_layout);
        mCategoryList = new ArrayList<>();
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_category);
        mTitleText = (TextView) findViewById(R.id.toolbar_title);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if(getIntent() != null) {
            List<Category> categoryList = getIntent().getParcelableArrayListExtra(AddCommodityActivity.KEY_DATA);
            setData(categoryList);
        }
        mTitleText.setText("商品分类");
        mToolbar.setNavigationIcon(R.mipmap.icon_back_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void setData(final List<Category> list) {
        if(mRecyclerView == null || mCategoryList == null) {
            return;
        }
        mAdapter = new CustomAdapter<Category>(list,
                this,
                R.layout.item_category_layout,
                null) {
            @Override
            public void onBindData(CustomHolder holder, int position, Category category) {
                holder.setText(R.id.text_category, category.getCategoryName());
            }
        };

        mAdapter.setOnItemClickListener(new CustomAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Event event = new Event();
                event.setData(list.get(position));
                EventBus.getDefault().post(event);
                finish();
            }
        });

        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mRecyclerView.setAdapter(mAdapter);
    }

}
